package org.gwt.project.tools;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.ui.plugin.AbstractUIPlugin;
import org.osgi.framework.BundleContext;

public class ToolsPlugin extends AbstractUIPlugin

{
    public static final String ID = "org.gwt.project.tools";

	public static final String JDT_BUILDER_ID = "org.eclipse.jdt.core.javabuilder";
	
	private static volatile ToolsPlugin plugin;

    public ToolsPlugin()
    {
    	super();
    	if(plugin==null) plugin = this;
    }
    
	@Override
	public void start(BundleContext context) throws Exception {
		super.start(context);
		plugin = this;
	}
	
	@Override
	public void stop(BundleContext context) throws Exception {
		plugin = null;
		super.stop(context);
	}    

    public static ToolsPlugin getInstance()
    {
    	if(plugin==null) new ToolsPlugin();
        return plugin;
    }

    public static void log( Exception e )
    {
        log(e.getMessage() + "" , e );
    }
    
    public static void log( String msg, Exception e )
    {
        log( new Status( IStatus.ERROR, ID, IStatus.OK, msg, e ) );
    }

    public static void log( IStatus status )
    {
    	try {
	        getInstance().getLog().log( status );
    	}
    	catch(NullPointerException e) {
    		e.printStackTrace();
    	}
    }

    public static void log( String msg )
    {
        log( new Status( IStatus.ERROR, ID, IStatus.OK, msg, null ) );
    }

    public static IStatus asOK( String msg )
    {
        return new Status( IStatus.OK, ID, msg );
    }
    
    public static IStatus asERROR( String msg )
    {
        return asERROR( msg, null );
    }

    public static IStatus asERROR( String msg, Throwable e )
    {
        return new Status( IStatus.ERROR, ID, 0, msg, e );
    }

    public static CoreException asException( String msg )
    {
        return asException( msg, null );
    }

    public static CoreException asException( String msg, Throwable e )
    {
        return new CoreException(asERROR(msg,e));
    }
    

}
